package com.wzh.borrow.mq;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.wzh.borrow.entity.BorrowEntity;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
/**
 * @author WZH
 * @title: Consumer
 * @projectName t1_1.22
 * @description:
 * @date 2022/1/22
 */
@Component
public class Consumer {
    @Autowired
    private RestHighLevelClient client;

    @RabbitListener(queues = "msg")
    public void take01(BorrowEntity borrow, Message message, Channel channel) throws IOException {
        IndexRequest request = new IndexRequest("borrow");
        request.id(borrow.getBid().toString());
        String jsonString = JSON.toJSONString(borrow);
        request.source(jsonString, XContentType.JSON);
        IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
